package com.filldream.fastboot.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;

/**
 * @author RickSun
 * @Description Swagger配置文件
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Value("${project.swagger.enable}")
	private boolean enable;

	private final static String baseUrl = "com.filldream.fastboot.web.controller";

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enable).select()
				.apis(RequestHandlerSelectors.basePackage(baseUrl)).paths(PathSelectors.any())
				.build().groupName("1-全部-1").globalOperationParameters(putParamter());
	}

	@Bean
	public Docket createRestApiApp() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enable).select()
				.apis(RequestHandlerSelectors.basePackage(baseUrl + ".app")).paths(PathSelectors.any())
				.build().groupName("2-APP接口-2").globalOperationParameters(putParamter());
	}

	@Bean
	public Docket createRestApiPc() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enable).select()
				.apis(RequestHandlerSelectors.basePackage(baseUrl + ".pc")).paths(PathSelectors.any())
				.build().groupName("3-PC接口-3").globalOperationParameters(putParamter());
	}

	@Bean
	public Docket createRestApiCommon() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enable).select()
				.apis(RequestHandlerSelectors.basePackage(baseUrl + ".common")).paths(PathSelectors.any())
				.build().groupName("4-公用接口-4").globalOperationParameters(putParamter());
	}


	/**
	 * @author RickSun
	 * @Description 添加header参数
	 **/
	private List<Parameter> putParamter() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		ParameterBuilder tokenPar2 = new ParameterBuilder();

		List<Parameter> pars = new ArrayList<>();
		tokenPar.name("token").description("登录token").modelRef(new ModelRef("string")).
				parameterType("header").required(false).build();
		tokenPar2.name("passToken").description("鉴权token").modelRef(new ModelRef("string")).
				parameterType("header").required(false).build();

		pars.add(tokenPar.build());
		pars.add(tokenPar2.build());
		return pars;
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("iFillDream FastBoot")
				.description("一款基于SpringBoot的快速构建框架，致力于每个项目的高效开发.")
				.termsOfServiceUrl("https://gitee.com/sunalee/fastboot  https://4m.cn/8pHoi")
				.contact(new Contact("RickSun","https://4m.cn/8pHoi","sunalee@dingtalk.com"))
				.version("iFillDream FastBoot2.0")
				.build();
	}

}
